/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import easybooking.data.Airport;
import easybooking.data.Flight;
import easybooking.data.Plane;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author Tim Schroeder
 */
public class FlightDialog extends JDialog {

    private EBInterface ebi;
    private JPanel dialogPanel;
    private JPanel buttonPanel;
    private JButton btnOk;
    private JButton btnCancel;
    private Flight editableFlight;
    private Airport startAirp;
    private Airport destAirp;
    private Plane p;
    private Date date;
    private JLabel lblStartAirp;
    private JTextField txfStartAirp = new JTextField();
    private JLabel lblDestAirp;
    private JTextField txfDestAirp = new JTextField();
    private JButton btnStartAirpSelect;
    private JButton btnDestAirpSelect;
    private JLabel lblPlane;
    private JTextField txfPlane = new JTextField();
    private JButton btnPlaneSelect;
    private JLabel lblDate;
    private JTextField txfDate = new JTextField();;
    private JButton btnDateSelect;

    public FlightDialog(EBInterface ebif) {
        ebi = ebif;
        //generateComponents();
    }
    // Get Text Fields
/**
 * get the startairport of a flight
 * @return the startairport
 */
    public Airport getStartAirport() {
        return this.startAirp;
    }
/**
 * get the destination airport of a flight
 * @return the dest airport
 */
    public Airport getDestAirport() {
        return this.destAirp;
    }
/**
 * get the plane of a flight
 * @return the plane object of a flight
 */
    public Plane getPlane() {
        return this.p;
    }
/**
 * get the Date of a flight
 * @return the date of a flight
 */
    public Date getDate() {
        return this.date;
    }
/**
 * get the editable flight object
 * @return the flight object which should be edit
 */
    public Flight getEditableFlight() {
        return editableFlight;
    }
/**
 * sets the startairport into the startariport text field
 * @param startAirp is the start airport of the flight
 */
    public void setStartAirport(Airport startAirp) {
        try {
            this.startAirp = startAirp;
            txfStartAirp.setText(startAirp.getName());
        } catch (RemoteException ex) {
            Logger.getLogger(FlightDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
/**
 * sets the startairport into the startariport text field
 * @param destAirp is the destination airport of the flight
 */
    public void setDestAirport(Airport destAirp) {
        try {
            this.destAirp = destAirp;
            txfDestAirp.setText(destAirp.getName());
        } catch (RemoteException ex) {
            Logger.getLogger(FlightDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
/**
 * sets the plane into the plane text field
 * @param p is the plane of the flight
 */
    public void setPlane(Plane p) {
        try {
            this.p = p;
            txfPlane.setText(p.getName());
        } catch (RemoteException ex) {
            Logger.getLogger(FlightDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
/**
 * sets the date into the date text field
 * @param date is the date of the flight
 */
    public void setDate(Date date) {
        try {
            this.date = date;
            txfDate.setText(date.toString());
        } catch (Exception ex) {
            Logger.getLogger(FlightDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
/**
 * sets the atributs of the flight into the text fields
 * @param f is the flight object which atributs get instert into the text fields
 */
    public void setTextFields(Flight f) {
        try {
            txfStartAirp.setText(startAirp.getName());
            txfDestAirp.setText(destAirp.getName());
            txfDate.setText(date.toString());
            txfPlane.setText(p.getName());
        } catch (RemoteException ex) {
            Logger.getLogger(FlightDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //shows the dialog when needed
            /**
 * calls the generateComponents() method to create and show the FlightDialog
 * @param f is the flight object which should be edit
 */
    public void createAndShowDialog(Flight f) {
        editableFlight = f;
        generateComponents();
        btnOk.setName("editFlight");
        setTextFields(f);
        this.setResizable(true);
        this.setTitle("Edit Plane");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }
     /**
 * calls the generateComponents() method to create and show the FlightDialog
 */
    public void createAndShowDialog() {
        generateComponents();
        btnOk.setName("createFlight");
        this.setResizable(true);
        this.setTitle("Create Flight");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    // Preperes the dialog
             /**
 * Generates all components to create and display the GUI
 */
    private void generateComponents() {

        // Dialog Settings

        dialogPanel = new JPanel();


        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }



        dialogPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();



        lblStartAirp = new JLabel("Start Airport:");
        addComponent(dialogPanel, gbl, lblStartAirp, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
        txfStartAirp = new JTextField(20);
        txfStartAirp.setEditable(false);
        addComponent(dialogPanel, gbl, txfStartAirp, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);
        btnStartAirpSelect = new JButton("Select");
         btnStartAirpSelect.setName("startAirp");
        btnStartAirpSelect.setActionCommand("FlightDialogAirportSelect");
        btnStartAirpSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnStartAirpSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 0, 1, 1, 0, 0);

        lblDestAirp = new JLabel("Destination Airport:");
        addComponent(dialogPanel, gbl, lblDestAirp, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 1, 1, 1, 0, 0);
        txfDestAirp = new JTextField(20);
        txfDestAirp.setEditable(false);
        addComponent(dialogPanel, gbl, txfDestAirp, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 1, 1, 1, 0, 0);
        btnDestAirpSelect = new JButton("Select");
        btnDestAirpSelect.setName("destAirp");
        btnDestAirpSelect.setActionCommand("FlightDialogAirportSelect");
        btnDestAirpSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnDestAirpSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 1, 1, 1, 0, 0);
        
        lblDate = new JLabel("Date:");
        addComponent(dialogPanel, gbl, lblDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 1, 1, 0, 0);
        txfDate = new JTextField(20);
        txfDate.setEditable(false);
        addComponent(dialogPanel, gbl, txfDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 2, 1, 1, 0, 0);
        btnDateSelect = new JButton("Select");
        btnDateSelect.setActionCommand("FlightDialogDateSelect");
        btnDateSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnDateSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 2, 1, 1, 0, 0);


        lblPlane = new JLabel("Plane:");
        addComponent(dialogPanel, gbl, lblPlane, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 3, 1, 1, 0, 0);
        txfPlane = new JTextField(20);
        txfPlane.setEditable(false);
        addComponent(dialogPanel, gbl, txfPlane, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 3, 1, 1, 0, 0);
        btnPlaneSelect = new JButton("Select");
        btnPlaneSelect.setActionCommand("FlightDialogPlaneSelect");
        btnPlaneSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnPlaneSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 3, 1, 1, 0, 0);

        // adds to buttonPanel
        addComponent(dialogPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 6, 2, 1, 0, 0);

        // sets die dialogPanel as contentPane
        this.setContentPane(dialogPanel);


    }

    // Creates the button panel
        /**
 * Creates the buttons to commit with ok or cancel
 * @return a JPanel with 2 buttons
 */
    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();

        btnOk = new JButton("OK");
        btnCancel = new JButton("Cancel");

        // Sets the buttons the same size
        btnOk.setPreferredSize(btnCancel.getPreferredSize());

        // Registrate action listeners
        btnOk.setActionCommand("flightDialogOk");
        btnOk.addActionListener(ebi);

        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // Adds the buttons to the buttonPanel
        buttonPanel.add(btnOk);
        buttonPanel.add(btnCancel);
        return buttonPanel;
    }
/**
 * defines the layout of the GUI
 * @param cont container to which the component is added
 * @param gbl GridBagLayout object
 * @param c compontent which is added to the container
 * @param fill how to use free space in the cell around the component
 * @param anchor where to fix an align the component
 * @param x x position in the grid layout
 * @param y y position in the grid layout
 * @param width number of columns the element should spread
 * @param hight number of rows the element should spread
 * @param weightx handels the horizontal free space
 * @param weighty handels the vertical free space
 */
    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
